<?php
namespace app\ausagent\controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cookie;
use think\Controller;
use think\Config;
use Godok\Org\Auth;
use think\Loader;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use Qcloud\cos\Api;
class Renew extends Controller{
  public function _initialize(){
    if(!session('id')){
          // $this->error('您尚未登录系统',url('login/index')); 
      $this->redirect(\think\Request::instance()->root().'/ausagent/Login/index');
    }
    $csh = Db::name('operator')->where('id',session('oid'))->find();
        if($csh['initialization']==0){//判断是否进行了初始化,0表示没有,1表示已经进行过初始化了
          //return $this->fetch('initialization/index');
          $this->redirect(\think\Request::instance()->root().'/ausagent/Initialization/index');
        }

  }
  public function index(){
      $groupids = \Godok\Org\Auth::user('groupids');
        $data = ['groupname'=>'游客'];
        if( !empty($groupids) ) {
           // var_dump($groupids);die;
            $group = Db::name( Config::get('auth.table_group') )->where(['id' => $groupids[0]])->find();
            if ( $group ) {
                $data['groupname'] = $group['title'];
            }
        }
        $data['menu'] = $this->menu2Html( \Godok\Org\Auth::menu("ismenu=1 AND status=1") );

        return $this->fetch();
      return view();
    }

  public function renew(){
     $ret = Db::name('operator_order')
                ->alias('a')
                ->join("version b",'a.version = b.id')
                ->join("operator c",'a.oid = c.id')
                ->where('a.oid',session('oid'))
                ->where('a.is_delete',1)
                ->field('a.*,b.name as vname,b.vision,b.price,c.name as oname')
                ->order('create_time desc')
                ->find();
            $ver = Db::name('version')->where('id',$ret['version'])->find();
            $this->assign('ver',$ver);
            $this->assign('v',$ret);
            //历史订单记录
            $list = Db::name('operator_order')
                  ->alias('a')
                  ->join("version b",'a.version = b.id')
                  ->where('a.oid',session('oid'))
                  ->where('a.is_delete',1)
                  ->where('a.pay_status',1)
                  ->order('a.create_time desc')
                  ->field('a.*,b.name as vname,b.vision')
                  ->select();
            $this->assign('list',$list);
            
            return view();
  }

  /**
     * 格式化菜单
     */
    private  function menu2Html($list,$level = 0)
    {
        $menu = '';
        $menulevel = ['first','second','third'];
        if ( is_array($list) ) {
            if( $level > 2 ) {
                $level = 2;
            }
            $menu .= $level > 0  ? '<ul class="nav nav-'.$menulevel[$level].'-level">' : '';

            foreach ( $list as $li ) {
                $menu .= '<li>';
                if( empty($li['module']) || empty($li['controller']) || '*' == $li['module'] || '*' == $li['controller'] ) {
                    $menu .= '<a href="javascript:void(0)" >';
                } else {
                    $act = explode(',',$li['action']);
                    $href = \think\Request::instance()->root().'/'. $li['module'] . '/' . $li['controller'] . '/' . $act[0];
                    if( !empty($li['condition']) ) {
                        $href .= '?' . $li['condition'];
                    }
                    $menu .= '<a href="' . $href . '" class="J_menuItem">';
                }
    
                if ( !empty($li['icon']) ) {
                    $menu .= '<i class="' . $li['icon'] . '"></i> ';
                }
                $menu .= '<span class="nav-label">' . $li['title'] . '</span>';
                if ( isset($li['children']) ) {
                    $children = $this->menu2Html($li['children'], $level + 1 );
                    $menu .= ' <span class="fa arrow"></span>';
                } else {
                    $children = '';
                }
                $menu .= '</a>' . $children;
                $menu .= '</li>';
            }
    
            $menu .= $level > 0 ? '</ul>' : '';
            return $menu;
        } else {
            return '';
        }
    }
}
